#include "config.h"

#include "ms2wgen.h"

ecm_uint32	duration = 3600;
ecm_double	arrival_lambda = 0.22;
ecm_double	skew = 0.271;
ecm_uint32	n_medias = 1;
ecm_uint32	media_length = 100;
ecm_uint32	media_size = 10000000;

char	*output;

static void
usage(void)
{
	ecm_fprintf(ecm_stderr,
"Usage: ms2wgen [ <options> ]\n"
" Options:\n"
"     -c <conf>: configuration path\n"
"     -t <duration>: workload generation period(hh:mm:ss)\n"
"     -o <output>: output file path\n"
	);
}

static void
init_parse_args(int argc, char *argv[])
{
	int	c;

	while ((c = ecm_getopt(argc, argv, "ht:c:o:")) != -1) {
		switch(c) {
		case 'c':
			confpath = ecm_strdup(ecm_optarg);
			if (!ecm_access(confpath, ECM_F_OK)) {
				ERR("%s: does not exist", ecm_optarg);
				ecm_exit(1);
			}
			break;
		default:
			break;
		}
	}
	ecm_getopt_clear();
}

static void
parse_args(int argc, char *argv[])
{
	int	c;

	while ((c = ecm_getopt(argc, argv, "ht:c:o:")) != -1) {
		switch(c) {
		case 'h':
			usage();
			ecm_exit(0);
		case 'c':
			break;
		case 't':
			if ((duration = msl_parse_duration(ecm_optarg)) == 0) {
				ERR("%s: invalid duration format", ecm_optarg);
				ecm_exit(1);
			}
			break;
		case 'o':
			output = ecm_strdup(ecm_optarg);
			break;
		default:
			usage();
			ecm_exit(1);
		}
	}
}

static ECMBOOL
init_ms2wgen_pre(void)
{
	if (!init_wgenconf())
		return ECM_FALSE;
	return ECM_TRUE;
}

static ECMBOOL
init_ms2wgen_post(void)
{
	if (!init_wg_session())
		return ECM_FALSE;
	return ECM_TRUE;
}

int
main(int argc, char *argv[])
{
	msl_initialize();

	ecm_set_output_file(ecm_stderr);
	ecm_set_verbose_mode(ECM_TRUE);

	init_parse_args(argc, argv);
	if (!init_ms2wgen_pre()) {
		ERR("init_ms2wgen_pre() failed");
		return 2;
	}

	parse_args(argc, argv);

	if (!init_ms2wgen_post()) {
		ERR("init_ms2wgen_post() failed");
		return 3;
	}

	gen_logs();

	return 0;
}
